import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import  MIMEBase,MIMEMultipart
from email import encoders
#import base64
import time
import threading

##SMTP服务
mail_host="smtp.163.com"
mail_user="***"
mail_pwd="***"

sender='***@163.com' #发件人邮箱
receiver=['***@qq.com'] #收件人邮箱

title='发送附件'
content='这是你要求的文档'

def sendmail():
    message=MIMEMultipart()
    mesg=MIMEText(content,'plain','utf-8') #文本方式
    message['From']=sender
    message['To']=",".join(receiver)
    message['Subject']=title
    message.attach(mesg)
    with open('D:/file/***','rb')as f:
        mime=MIMEBase('application', 'octet-stream')
        mime.add_header('Content-Disposition','attachment',filename=('gbk','','***'))
        mime.add_header('Content-ID','<0>')
        mime.add_header('X-Attachment-Id','<0>') 
        mime.set_payload(f.read())
        encoders.encode_base64(mime)
        message.attach(mime)
        
    try:
        smtpObj=smtplib.SMTP_SSL(mail_host,465) #SSL
        smtpObj.login(sender,mail_pwd) #登录
        smtpObj.sendmail(sender,receiver,message.as_string())
        print('mail has been send successfully')
        print(time.ctime())
        smtpObj.quit()
    except smtplib.SMTPException as e:
        print(e)

    timer=threading.Timer(1800,sendmail)
    timer.start()

if __name__=='__main__':
    sendmail()